<?php

namespace System\Core;

use \System\Types;

/**
 * Description of Config
 *
 * @author elton.schivei
 */
class Config extends \System\Types\Object
{

    const GENERAL = \CFG_GENERAL;
    const DATABASE = \CFG_DATABASE;
    const HOOKS = \CFG_HOOKS;
    const ROUTES = \CFG_ROUTES;
    const LOGS = \CFG_LOGS;
    const SEGMENTS = \CFG_SEGMENTS;
    const URI = \CFG_URI;
    const LANG = \CFG_LANG;

    private static $config = array();

    function __construct()
    {
        parent::__construct();
    }

    /**
     * @param int $type
     * @param string $key
     * @param mixed $value
     * @return System\Core\ConfigItem
     */
    public static function Set($type, $key, $value)
    {
        $type = (int) $type;

        switch ($type)
        {
            case self::GENERAL:
            case self::DATABASE:
            case self::HOOKS:
            case self::ROUTES:
            case self::LOGS:
            case self::SEGMENTS:
            case self::URI:
            case self::LANG:
            case \CFG_GENERAL:
            case \CFG_DATABASE:
            case \CFG_HOOKS:
            case \CFG_ROUTES:
            case \CFG_LOGS:
            case \CFG_SEGMENTS:
            case \CFG_URI:
            case \CFG_LANG:
                if (empty(self::$config[$type]))
                    self::$config[$type] = array();

                self::$config[$type][] = array('key' => $key, 'value' => $value);
                $itens = new ConfigItems($type, new self());

                return $itens->Get($key);
                break;

            default:
                return null;
                break;
        }
    }

    /**
     * @param int $type
     * @return System\Core\ConfigItems
     */
    public static function Get($type)
    {
        $type = (int) $type;

        switch ($type)
        {
            case self::GENERAL:
            case self::DATABASE:
            case self::HOOKS:
            case self::ROUTES:
            case self::LOGS:
            case self::SEGMENTS:
            case self::URI:
            case self::LANG:
            case \CFG_GENERAL:
            case \CFG_DATABASE:
            case \CFG_HOOKS:
            case \CFG_ROUTES:
            case \CFG_LOGS:
            case \CFG_SEGMENTS:
            case \CFG_URI:
            case \CFG_LANG:
                return new \System\Core\ConfigItems($type, new self());
                break;

            default:
                return null;
                break;
        }
    }

    /**
     * @param int $type
     * @param string $name
     * @return array
     */
    public static function Remove($type, $name)
    {
        $type = (int) $type;

        switch ($type)
        {
            case self::GENERAL:
            case self::DATABASE:
            case self::HOOKS:
            case self::ROUTES:
            case self::LOGS:
            case self::SEGMENTS:
            case self::URI:
            case self::LANG:
            case \CFG_GENERAL:
            case \CFG_DATABASE:
            case \CFG_HOOKS:
            case \CFG_ROUTES:
            case \CFG_LOGS:
            case \CFG_SEGMENTS:
            case \CFG_URI:
            case \CFG_LANG:
                $conf = self::$config[$type][$name];
                unset(self::$config[$type][$name]);
                return $conf;
                break;

            default:
                return null;
                break;
        }
    }

    /**
     * @param int $type
     * @return int
     */
    public static function Count($type)
    {
        $type = (int) $type;

        switch ($type)
        {
            case self::GENERAL:
            case self::DATABASE:
            case self::HOOKS:
            case self::ROUTES:
            case self::LOGS:
            case self::SEGMENTS:
            case self::URI:
            case self::LANG:
            case \CFG_GENERAL:
            case \CFG_DATABASE:
            case \CFG_HOOKS:
            case \CFG_ROUTES:
            case \CFG_LOGS:
            case \CFG_SEGMENTS:
            case \CFG_URI:
            case \CFG_LANG:
                return \count(self::$config[$type]);
                break;

            default:
                return null;
                break;
        }
    }

    /**
     * @param int $type
     * @param string $name
     * @return mixed
     */
    public static function GetValue($type, $name)
    {
        $type = (int) $type;

        switch ($type)
        {
            case self::GENERAL:
            case self::DATABASE:
            case self::HOOKS:
            case self::ROUTES:
            case self::LOGS:
            case self::SEGMENTS:
            case self::URI:
            case self::LANG:
            case \CFG_GENERAL:
            case \CFG_DATABASE:
            case \CFG_HOOKS:
            case \CFG_ROUTES:
            case \CFG_LOGS:
            case \CFG_SEGMENTS:
            case \CFG_URI:
            case \CFG_LANG:
                $value = null;
                if (!empty(self::$config[$type]) && \count(self::$config[$type]))
                {
                    foreach (self::$config[$type] as $item)
                    {
                        if ($item['key'] == $name)
                        {
                            $value = $item['value'];
                            break;
                        }
                    }
                }

                return($value? : null);
                break;

            default:
                return null;
                break;
        }
    }

}
